﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Millionaire;

namespace WindowsFormsApplication1.BLL
{
    public class LuaTuoiBLL : BaseBLL, API.ILuaTuoiBLL
    {
        #region ILuaTuoiBLL Members

        public bool AddNew(Millionaire.LuaTuoi lt)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    ctxt.LuaTuois.InsertOnSubmit(lt);
                    ctxt.SubmitChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Update(Millionaire.LuaTuoi lt)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LuaTuoi edit = ctxt.LuaTuois.SingleOrDefault(x => x.MaLuaTuoi == lt.MaLuaTuoi);
                    if (edit != null)
                    {
                        edit.TenLuaTuoi = lt.TenLuaTuoi;
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public bool Delete(Millionaire.LuaTuoi lt)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                try
                {
                    LuaTuoi delObj = ctxt.LuaTuois.SingleOrDefault(x => x.MaLuaTuoi == lt.MaLuaTuoi);
                    if (delObj != null)
                    {
                        ctxt.LuaTuois.DeleteOnSubmit(delObj);
                        ctxt.SubmitChanges();
                        return true;
                    }
                    else
                        return false;

                }
                catch (Exception)
                {
                    return false;
                }

            }
        }

        public Millionaire.LuaTuoi GetByID(int idLuaTuoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LuaTuois.SingleOrDefault(x => x.MaLuaTuoi == idLuaTuoi);
            }
        }

        public List<Millionaire.LuaTuoi> GetAll()
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.LuaTuois.ToList();
            }
        }

        public Millionaire.LuaTuoi GetByCauHoi(int idCauHoi)
        {
            using (var ctxt = new MillionaireDataContext(connString))
            {
                ctxt.ObjectTrackingEnabled = false;
                return ctxt.CauHois.Where(x => x.MaCauHoi == idCauHoi).Select(x => x.LuaTuoi).Single();
            }
        }

        #endregion
    }
}
